package sibu.cn.mydemo;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import sibu.cn.mydemo.androidmaterialdesign.androidui.AndroidUIActivity;
import sibu.cn.mydemo.androidmaterialdesign.floataction.FloatActionActivity;
import sibu.cn.mydemo.annotation.AnnotateActivity;
import sibu.cn.mydemo.camera.CameraActivity;
import sibu.cn.mydemo.charts.sample1.ChartMainActivity;
import sibu.cn.mydemo.charts.sample1.MyChartViewActivity;
import sibu.cn.mydemo.customview.draglayout.IndexActivity;
import sibu.cn.mydemo.customview.flowview.FlowViewActivity;
import sibu.cn.mydemo.customview.loadingview.LoadViewActivity;
import sibu.cn.mydemo.customview.magnifierdemo.MagnifierActivity;
import sibu.cn.mydemo.customview.popupwindow.PopupWindowLocationActivity;
import sibu.cn.mydemo.customview.textandedit.EmojiTextViewSampleActivity;
import sibu.cn.mydemo.greendao.GreenDaoActivity;
import sibu.cn.mydemo.newsstand.NewsStandActivity;
import sibu.cn.mydemo.sensor.snowdemo.SnowActivity;
import sibu.cn.mydemo.customview.zoomimageview.ZoomImageViewActivity;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link DetailFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 * Use the {@link DetailFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DetailFragment extends Fragment implements MainActivity.ChangeFragmentCallBack {


    private String arr[] = new String[]{
            "缩放的ZoomImageView","形式多样的loadingView","放大镜的效果1","popupwindow的弹出位置"
            ,"雪花飘落","显示表情与文字的textView","drag_layout"
    };

    private String empty[] = new String[]{};

    private String annotations[] = new String[]{
            "bindView"
    };

    private String sensor[] = new String[]{
            "sensorSnow"
    };

    private String newsStand[] = new String[]{
      "PlayNewsStandDemo"
    };

    private String materialDisign[] = new String[]{
            "androidui","float_action"
    };

    private String dataBase[] = new String[]{
      "greendao"
    };
    private String charts[] = new String[]{
            "demo1","demo2"
    };

    private String cameras[] = new String[]{
            "camera1"
    };


    private static final String INDEX = "index";
    private static final String ARG_PARAM2 = "param2";

    private int type = 0;
    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;
    private View view;
    private ListView listView;
    private OnFragmentInteractionListener mListener;
    private ListViewAdapter listViewAdapter;


    public static DetailFragment newInstance(String param1, String param2) {
        DetailFragment fragment = new DetailFragment();
        Bundle args = new Bundle();
        args.putString(INDEX, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    public DetailFragment() {
        // Required empty public constructor
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        Log.e("fragment", "onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("fragment", "onCreate");
        if (getArguments() != null) {
            mParam1 = getArguments().getString(INDEX);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
        ((MainActivity) getActivity()).setChangeFragmentCallBack(this);//回调接口修改数据
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        Log.e("fragment", "onCreateView");
        view = inflater.inflate(R.layout.fragment_detail, container, false);
        initView();
        return view;
    }

    public void starttoActivity(Context context,Class activity){
        Intent intent = new Intent( context,activity);
        context.startActivity(intent);
    }

    public void initView() {
        listView = (ListView) view.findViewById(R.id.fragment_listview);
        listViewAdapter = new ListViewAdapter(getActivity(), arr);
        listView.setAdapter(listViewAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (type == Constant.CUSTOUMVIEW) {//自定义view
                    switch (position) {
                        case 0:
                            ZoomImageViewActivity.jumpZoomImageViewActivity(getActivity());
                            break;
                        case 1:
                            LoadViewActivity.jumpLoadViewActivity(getActivity());
                            break;
                        case 2:
                            MagnifierActivity.jumpMagnifierActivity(getActivity());
                            break;
                        case 3:
                            PopupWindowLocationActivity.jumpPopupLoacationActivity(getActivity());
                            break;
                        case 4:
                            FlowViewActivity.jumpFlowViewActivity(getActivity());
                            break;
                        case 5:
                            starttoActivity(getActivity(), EmojiTextViewSampleActivity.class);
                            break;
                        case 6:
                            starttoActivity(getActivity(), IndexActivity.class);
                            break;
                    }
                }
                if (type == Constant.ANNOTATION) {//注解类
                    switch (position) {
                        case 0:
                            AnnotateActivity.jumpAnnotateActivity(getActivity());
                            break;
                    }
                }
                if (type == Constant.SENSOR) {//传感器类
                    switch (position) {
                        case 0:
                            SnowActivity.jumpSnowActivity(getActivity());
                            break;
                    }
                }
                if (type == Constant.NEWSSTAND) {//资讯类新闻客户端框架
                    switch (position) {
                        case 0:
                            NewsStandActivity.jumpNewsStandActivity(getActivity());
                            break;
                    }
                }
                if (type == Constant.MATERIALDESIGN) {
                    switch (position) {
                        case 0:
                            AndroidUIActivity.jumpAndroidUIActivity(getActivity());
                            break;
                        case 1:
                            FloatActionActivity.jumpFloatActionActivity(getActivity());
                            break;
                    }
                }
                if (type == Constant.DATABASE) {//数据库
                    switch (position) {
                        case 0:
                            GreenDaoActivity.jumpGreenDaoActivity(getActivity());
                            break;
                    }
                }
                if (type == Constant.CHART) {//chart
                    switch (position) {
                        case 0:
                            ChartMainActivity.jumpChartMainActivity(getActivity());
                            break;
                        case 1:
                            MyChartViewActivity.jumpMyChartViewActivity(getActivity());
                            break;
                    }
                }

                if(type == Constant.CAMERA){//相机
                    switch (position) {
                        case 0:
                           starttoActivity(getActivity(), CameraActivity.class);
                            break;

                    }
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e("fragment", "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.e("fragment", "onPause");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.e("fragment", "onStart");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.e("fragment", "onStop");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("fragment", "onStop");
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.e("fragment", "onAttachactivity");
      /*  try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }*/
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.e("fragment", "onDetach");
        mListener = null;
    }

    @Override
    public void changeData(int index) {//修改fragment数据
        type = index;
        switch (index) {
            case 0:
                listViewAdapter.setArr(arr);
                break;
            case 1:
                listViewAdapter.setArr(annotations);
                break;
            case 2:
                listViewAdapter.setArr(sensor);
                break;
            case 3:
                listViewAdapter.setArr(newsStand);
                break;
            case 4:
                listViewAdapter.setArr(materialDisign);
                break;
            case 5:
                break;
            case 6:
                listViewAdapter.setArr(charts);
                break;
            case 7:
                listViewAdapter.setArr(dataBase);
                break;
            case 8:
                listViewAdapter.setArr(cameras);
                break;
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }

}
